package com.huike.game.fragment;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.JsonRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.huike.game.Bean.Bean22;
import com.huike.game.R;
import com.huike.game.adapter.MyAdpter;
import com.huike.game.api.Api;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class Game22_person extends Fragment {

    Context context;
    private ImageView mIvGame22Person;
    private TextView mTvGame22Name;
    private TextView mTvGame22Sex;
    private TextView mTvGame22Phone;
    private TextView mTvGame22ID;
    private TextView mTvGame22Time;
    private GridView mLvGame22Person;

    MyAdpter myAdpter=null;

    ArrayList<Bean22.DataBean> dataList = new ArrayList<>();
    RequestQueue requestQueue = null;
    private String TAG = "log";

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getContext();
        Log.e("---", "Game22_person");
        requestQueue = Volley.newRequestQueue(context);
        getData();
    }

    private void getData() {
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
                Request.Method.POST,
                Api.USER_INFO,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject jsonObject) {
                        Log.e(TAG, "onResponse: " + jsonObject.toString());
                        Gson gson = new Gson();
                        Bean22 bean22 = gson.fromJson(jsonObject.toString(), Bean22.class);
                        List<Bean22.DataBean> data = bean22.getData();

                        for (Bean22.DataBean a:data){
                            Log.e(TAG, "onResponse: "+a.toString() );
                            dataList.add(a);
                        }
                        myAdpter.notifyDataSetChanged();
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        Log.e(TAG, "onErrorResponse: " + volleyError.getMessage());
                    }
                }
        );

        requestQueue.add(jsonObjectRequest);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        final View inflate = View.inflate(context, R.layout.game22_person, null);


        mIvGame22Person = (ImageView) inflate.findViewById(R.id.iv_game22_person);
        mTvGame22Name = (TextView) inflate.findViewById(R.id.tv_game22_name);
        mTvGame22Sex = (TextView) inflate.findViewById(R.id.tv_game22_sex);
        mTvGame22Phone = (TextView) inflate.findViewById(R.id.tv_game22_phone);
        mTvGame22ID = (TextView) inflate.findViewById(R.id.tv_game22_ID);
        mTvGame22Time = (TextView) inflate.findViewById(R.id.tv_game22_time);
        mLvGame22Person = inflate.findViewById(R.id.lv_game22_person);


        myAdpter = new MyAdpter(dataList,context);

        mLvGame22Person.setAdapter(myAdpter);
        return inflate;
    }
}
